<?php
class CouponHistoriesController extends AppController {
	var $name = 'CouponHistories';

    public function admin_search ($member_code) {
        if (!$member_code) {
            $json = array(
                'status' => false,
                'mesg'   => __('User Id does not found', true)
            );
            $this->response($json);
            return;
        }

        $this->loadModel('PiVoucherHeader');
        $data = $this->PiVoucherHeader->find('all', array(
            'conditions' => array('PiVoucherDetail.MemberCode' => $member_code)
        ));
        $list = array();

        foreach ($data as $i=>$coupon) {
            $c = $this->CouponHistory->find('first', array(
                'conditions' => 'coupon_id='.$coupon['PiVoucherDetail']['piVoucherHeaderID'],
                'recursive'  => -1,
                'fields'     => 'balance, created',
                'order'      => 'id DESC'
            ));

            $balance = ($c != false) ? $c['CouponHistory']['balance'] : $coupon['PiVoucherDetail']['Debit'];
            $lastUse = ($c != false) ? $this->serverToClientTime($c['CouponHistory']['created'], 'd/m/Y h:ia') : '-';

            $list[$i] = array(
                'id'      => $coupon['PiVoucherDetail']['piVoucherHeaderID'],
                'code'    => $coupon['PiVoucherDetail']['documentno'],
                'total'   => number_format($coupon['PiVoucherDetail']['Debit'], 2),
                'balance' => number_format($balance, 2),
                'created' => date_format($coupon['PiVoucherHeader']['TransactionDate'], 'd/m/Y h:ia'),
                'lastUse' => $lastUse
            );
        }

        $json = array(
            'status' => true,
            'list'   => $list
        );
        $this->response($json);
    } // admin_search

    public function coupon_list ($ax='') {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($ax || $this->RequestHandler->isAjax()) {
            $id   = $this->Session->read('Member.id');
            $code = $this->Session->read('Member.code');

            $this->loadModel('PiVoucherHeader');
            /*$this->PiVoucherDetail->bindModel(
                array('hasOne' => array(
                    'PiVoucherHeader' => array(
                        'className'  => 'PiVoucherHeader'
                    )
                )
            );*/
            $data = $this->PiVoucherHeader->coupon_list($code);
            $used = $this->CouponHistory->usage_history($id);

            $summary = array(
                'totalNewCoupon'   => 0,
                'totalValueCoupon' => 0.00,
                'totalPaidCoupon'  => 0,
                'totalValuePaid'   => 0.00,
                'balance'          => 0.00
            );

            if (is_array($used) && is_array($data)) {
                $data = array_merge($data, $used);

                foreach ($data as $i=>$rs) {
                    $sorttime[$i] = $rs['sortTime'];

                    if ($rs['effective'] == '') { // used coupon
                        $summary['totalPaidCoupon'] += 1;
                        $summary['totalValuePaid']  += abs($rs['amount']);
                    } else {
                        $summary['totalNewCoupon']   += 1;
                        $summary['totalValueCoupon'] += $rs['amount'];
                    }
                }

                $summary['totalValueCoupon'] = number_format($summary['totalValueCoupon'], 2);
                $summary['totalValuePaid']   = number_format($summary['totalValuePaid'], 2);
                $summary['balance']          = number_format($summary['totalValueCoupon'] - $summary['totalValuePaid'], 2);

                array_multisort($sorttime, $data);
            }

            $this->set('data', $data);
            $this->set('summary', $summary);
            $this->render('coupon_tbl', 'blank');
        } else {
            $this->javascripts = am($this->javascripts, 'coupon_list');
            $this->pageTitle   = __('Product Coupon', true);

            $this->set('pageTitle', $this->pageTitle);
        }
    } // coupon_list
} // CouponHistoriesController
?>